<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_AddNewWebsiteAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$formValues = $this->getFormValues("form");

		$this->addToModel("href", $this->getRequestParameter("href"));
        $this->addToModel('selectedLang', strtoupper(f_core_Context::getInstance()->getBrowserLanguage()));

		if(count($formValues) > 0)
		{
			$errors = $this->checkForm($formValues);
			
			if(count($errors) == 0)
			{					
				$newWebsite = m_website_documents_Website::createNew();
				
				$rootNode = m_backoffice_services_RootNodeService::getInstance()->getModuleRootNode("website", $formValues["lang"]);
				
				if(!$rootNode->isTranslated())
				{
					$rootNode	->setAuthor($this->getUser()->getLogin())
								->setLabel("rootNode ".$formValues['lang']);								
				}
				
				$newWebsite ->setAuthor($this->getUser()->getLogin())
							->setLabel($formValues["label"])
							->setUrl($formValues["url"])
							->setLang($formValues["lang"])
                            ->setDisplayLang($formValues['lang'])
                            ->save();

				$rootNode->addWebsite($newWebsite)->save();
				
				$errors["status"] = "success";
				$this->addToModel("values", $formValues);
				$this->addToModel("errors", $errors);
				$this->addToModel("website", $newWebsite);	
			}
			else
			{
				$errors["status"] = "failed";
				$this->addToModel("errors", $errors);
				$this->addToModel("values", $formValues);
                $this->addToModel('selectedLang', strtoupper($formValues['lang']));
			}
		}
        
        
        $this->addToModel("isoCode",f_locale_Manager::getIsoCodes());
        $this->addToModel("frontLangs", $this->getContext()->getAvailableDisplayLangs(false));
		$this->setView('success');
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo", "website_Add"));
	}
	
	private function checkForm($formValues)
	{
		$errors = array();
		
		if($formValues["lang"] == "")
		{
			$errors["lang"] = $this->getLocale('website.errors.required');
		}
		
		if($formValues["label"] == "")
		{
			$errors["label"] = $this->getLocale('website.errors.required');
		}		
					
		if($formValues["url"] == "")
		{
			$errors["url"] = $this->getLocale('website.errors.required');
		}

        if(!preg_match("/^([a-zA-Z1-9.~_,-])*$/",$formValues["url"]))
        {
            $errors["url"] = $this->getLocale('website.errors.invalidUrlChar');
        }
        
		if($this->checkIfUrlExists($formValues["url"]))
		{
			$errors["url"] = $this->getLocale('website.errors.duplicateURL');
		}
		
		return $errors;
	}
	
	private function checkIfUrlExists($url)
	{
        $value = m_website_services_URLService::urlExists($url, null, null, "modules_website/website");
		return $value;
	}
}